

#第十二章 武装飞船
import sys,os
import pygame
import game_utils 
from setting import Setting
from ship import Ship
from alien import Alien
from pygame.sprite import Group
from pygame.surface import Surface

from game_stats import Game_stats
from component.button import Button
from component.label import Label
from views.scoreView import ScoreView


def run_game():
    alien_set = Setting()
    
    pygame.init() #初始化引擎

    screen = pygame.display.set_mode(alien_set.screen_size) #设置屏幕大小 Surface 元素 可以认为所有显示对象都是 Surface 类
    #Surface(screen).
    #print("screen",type(screen))
    pygame.display.set_caption("Alien Invasion 外星人入侵") #设置窗口的标题字符串
    stats = Game_stats(alien_set)
    root = os.path.dirname(__file__)
    alien_set.rootUrl = root
    #print("root",root)
    ship = Ship(screen,root) #创建飞船

    rgbColor = alien_set.bg_color
    bullets_group = Group() #储存子弹的容器 类似 list
    aliens_group = Group()
    game_utils.create_aliens(alien_set,screen,aliens_group)

    btn = Button(screen,"开始")
    scoreView = ScoreView(screen) 
    
    while True:
        #监听各类事件
        screen.fill(alien_set.bg_color) #填充屏幕颜色 
        game_utils.check_event(alien_set,ship,screen,bullets_group,stats,btn)
        if stats.game_active:                         
            game_utils.update_bullets(bullets_group)    
            game_utils.update_aliens(aliens_group,ship,bullets_group,stats,screen,alien_set,scoreView)
        else:
            btn.draw_button()
        scoreView.draw()
        
        game_utils.update_screen(alien_set,ship,screen)
       

run_game()
